package com.ruoyi.business.controller;

import com.ruoyi.business.domain.BusCbsxx;
import com.ruoyi.business.domain.statistics.StatisticsBigDecimalBO;
import com.ruoyi.business.domain.statistics.StatisticsIntBO;
import com.ruoyi.business.domain.vo.BusCbsxxSaveVO;
import com.ruoyi.business.domain.vo.BusCbsxxVO;
import com.ruoyi.business.service.IBusCbsgrxxService;
import com.ruoyi.business.service.IBusCbsxxService;
import com.ruoyi.business.service.IBusFyglService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 承包商信息Controller
 *
 * @author yaowei
 * @date 2021-06-30
 */
@Api(description = "承包商信息")
@RestController
@RequestMapping("/business/cbsxx")
public class BusCbsxxController extends BaseController {

    @Autowired
    private IBusCbsxxService busCbsxxService;
    @Autowired
    private IBusCbsgrxxService busCbsgrxxService;
    @Autowired
    private IBusFyglService busFyglService;


    /**
     * 查询承包商信息列表
     */
    @ApiOperation("获取当前承包商用户信息")
    @GetMapping("/currentCbsxx")
    public BusCbsxx currentUserInfo() {
        Long userId = SecurityUtils.getUserId();
        BusCbsxx busCbsxx = new BusCbsxx();
        busCbsxx.setXrrId(userId);
        List<BusCbsxx> list = busCbsxxService.selectBusCbsxxList(busCbsxx);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * 查询承包商信息列表
     */
    @ApiOperation("承包商列表")
    @GetMapping("/list")
    public TableDataInfo list(BusCbsxx busCbsxx, String year) {
        startPage();
        List<BusCbsxx> list = busCbsxxService.selectBusCbsxxList(busCbsxx);
        List<BusCbsxxVO> rsList = new ArrayList<>(list.size());
        list.forEach(e -> {
            Long cbsId = e.getId();
            // 工作状态统计
            BusCbsxxVO busCbsxxVO = BeanUtils.copyBean(e, new BusCbsxxVO());
            List<StatisticsIntBO> gzztList = busCbsgrxxService.gzztStatistics(cbsId);
            busCbsxxVO.setGrgzztStatistics(gzztList);
            // 费用情况统计
            List<StatisticsBigDecimalBO> fyqkList = busFyglService.statisticsByCbsId(cbsId, year);
            busCbsxxVO.setFyqkStatistics(fyqkList);
            rsList.add(busCbsxxVO);
        });
        return getDataTable(list, rsList);
    }

    /**
     * 导出承包商信息列表
     */
    @Log(title = "承包商信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusCbsxx busCbsxx) {
        List<BusCbsxx> list = busCbsxxService.selectBusCbsxxList(busCbsxx);
        ExcelUtil<BusCbsxx> util = new ExcelUtil<BusCbsxx>(BusCbsxx.class);
        return util.exportExcel(list, "cbsxx");
    }

    /**
     * 获取承包商信息详细信息
     */
    @ApiOperation("根据主键ID查询承包商信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(busCbsxxService.selectBusCbsxxById(id));
    }

    /**
     * 新增承包商信息
     */
    @ApiOperation("承包商新增/修改")
    @Log(title = "承包商信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusCbsxxSaveVO busCbsxx) {
        Long cbsId = busCbsxx.getId();
        if (cbsId == null) {
            return toAjax(busCbsxxService.insertBusCbsxx(busCbsxx));
        } else {
            return toAjax(busCbsxxService.updateBusCbsxx(busCbsxx));
        }
    }

    /**
     * 删除承包商信息
     */
    @ApiOperation("承包商批量删除")
    @Log(title = "承包商信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(busCbsxxService.deleteBusCbsxxByIds(ids));
    }
}
